package me.chanjar.weixin.mp.api.impl;

import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class ComponentWxMpRedisConfigImpl extends WxMpDefaultConfigImpl {
    private static final String ACCESS_KEY = "wx:component:";
    private final JedisPool jedisPool;
    private String accessTokenKey;

    public ComponentWxMpRedisConfigImpl(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    @Override
    public void setAppId(String appId) {
        super.setAppId(appId);
        this.accessTokenKey = ACCESS_KEY.concat("access_token:").concat(appId);
    }

    private String getTicketRedisKey(TicketType type) {
        return String.format(ACCESS_KEY.concat("ticket:key:%s:%s"), this.appId, type.getCode());
    }

    @Override
    public String getAccessToken() {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var2 = null;

        String var3;
        try {
            var3 = jedis.get(this.accessTokenKey);
        } catch (Throwable var12) {
            var2 = var12;
            throw var12;
        } finally {
            if (jedis != null) {
                if (var2 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var11) {
                        var2.addSuppressed(var11);
                    }
                } else {
                    jedis.close();
                }
            }

        }

        return var3;
    }

    @Override
    public boolean isAccessTokenExpired() {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var2 = null;

        boolean var3;
        try {
            var3 = jedis.ttl(this.accessTokenKey) < 2L;
        } catch (Throwable var12) {
            var2 = var12;
            throw var12;
        } finally {
            if (jedis != null) {
                if (var2 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var11) {
                        var2.addSuppressed(var11);
                    }
                } else {
                    jedis.close();
                }
            }

        }

        return var3;
    }

    @Override
    public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var4 = null;

        try {
            jedis.setex(this.accessTokenKey, expiresInSeconds - 200, accessToken);
        } catch (Throwable var13) {
            var4 = var13;
            throw var13;
        } finally {
            if (jedis != null) {
                if (var4 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var12) {
                        var4.addSuppressed(var12);
                    }
                } else {
                    jedis.close();
                }
            }

        }

    }

    @Override
    public void expireAccessToken() {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var2 = null;

        try {
            jedis.expire(this.accessTokenKey, 0);
        } catch (Throwable var11) {
            var2 = var11;
            throw var11;
        } finally {
            if (jedis != null) {
                if (var2 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var10) {
                        var2.addSuppressed(var10);
                    }
                } else {
                    jedis.close();
                }
            }

        }

    }

    @Override
    public String getTicket(TicketType type) {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var3 = null;

        String var4;
        try {
            var4 = jedis.get(this.getTicketRedisKey(type));
        } catch (Throwable var13) {
            var3 = var13;
            throw var13;
        } finally {
            if (jedis != null) {
                if (var3 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var12) {
                        var3.addSuppressed(var12);
                    }
                } else {
                    jedis.close();
                }
            }

        }

        return var4;
    }

    @Override
    public boolean isTicketExpired(TicketType type) {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var3 = null;

        boolean var4;
        try {
            var4 = jedis.ttl(this.getTicketRedisKey(type)) < 2L;
        } catch (Throwable var13) {
            var3 = var13;
            throw var13;
        } finally {
            if (jedis != null) {
                if (var3 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var12) {
                        var3.addSuppressed(var12);
                    }
                } else {
                    jedis.close();
                }
            }

        }

        return var4;
    }

    @Override
    public synchronized void updateTicket(TicketType type, String jsapiTicket, int expiresInSeconds) {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var5 = null;

        try {
            jedis.setex(this.getTicketRedisKey(type), expiresInSeconds - 200, jsapiTicket);
        } catch (Throwable var14) {
            var5 = var14;
            throw var14;
        } finally {
            if (jedis != null) {
                if (var5 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var13) {
                        var5.addSuppressed(var13);
                    }
                } else {
                    jedis.close();
                }
            }

        }

    }

    @Override
    public void expireTicket(TicketType type) {
        Jedis jedis = this.jedisPool.getResource();
        Throwable var3 = null;

        try {
            jedis.expire(this.getTicketRedisKey(type), 0);
        } catch (Throwable var12) {
            var3 = var12;
            throw var12;
        } finally {
            if (jedis != null) {
                if (var3 != null) {
                    try {
                        jedis.close();
                    } catch (Throwable var11) {
                        var3.addSuppressed(var11);
                    }
                } else {
                    jedis.close();
                }
            }

        }

    }
}
